package ac.beiwo.baas.user.service;

import ac.beiwo.baas.common.entity.SimpleCode;
import ac.beiwo.baas.common.entity.SimpleError;
import ac.beiwo.baas.object.entity.BaasObject;
import ac.beiwo.baas.object.service.ObjectService;
import ac.beiwo.baas.user.entity.BaasInstallation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Staryet on 15/8/13.
 */
@Service
public class InstallationService {

    public static String INSTALLATION_CLASS_NAME = "_Installation";
    @Autowired
    private ObjectService objectService;

    /**
     * 注册设备
     *
     * @param installation 设备信息
     * @return
     * @throws SimpleError
     */
    public String register(BaasInstallation installation) throws SimpleError {
        BaasObject exist = objectService.get(INSTALLATION_CLASS_NAME, installation.getId());
        if (exist != null) {
            //用户已存在
            throw new SimpleError(SimpleCode.USER_ALREADY_EXIST);
        }
        //禁止设置ACL字段
        installation.remove("acl");
        return objectService.insert(INSTALLATION_CLASS_NAME, installation, null, true);
    }

}
